<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="状态" prop="filterStatus">
        <el-select
          v-model="queryParams.filterStatus"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in statuses"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="keyWord">
        <el-input placeholder="请输入内容" v-model.trim="keyWord">
          <template #prepend>
            <el-select v-model="kType" placeholder="请选择" class="!w-120px">
              <el-option label="银联账户" value="ownMemberMatCode" />
              <el-option label="租客姓名" value="memberUserName" />
              <el-option label="租住地址" value="rentAddrName" />
            </el-select>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="租客/地址" align="left">
        <template #default="scope">
          <div>租客：{{ scope.row.memberUserName }}</div>
          <div>地址：{{ scope.row.rentAddrName }}</div>
          <div>分账：{{ scope.row.ownMemberMatCode }}</div>
        </template>
      </el-table-column>
      <el-table-column label="申请" align="left" width="170px">
        <template #default="scope">
          <div>抄表余额：{{ divide(scope.row.reqReadoutBalance, 100) }}</div>
          <div>手续费：{{ divide(scope.row.payRefundAmount, 100) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="审核" align="left" width="170px">
        <template #default="scope">
          <div>抄表余额：{{ divide(scope.row.refundReadoutBalance, 100) }}</div>
          <div>退款总金额：{{ divide(scope.row.refundAmount, 100) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="left" width="240px">
        <template #default="scope">
          <div>申请时间：{{ formatDate(scope.row.createTime) }}</div>
          <div>退款成功：{{ formatDate(scope.row.successTime) }}</div>
          <div>失效时间：{{ formatDate(scope.row.expireTime) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="120px">
        <template #default="scope">
          <div
            >审核：<dict-tag :type="DICT_TYPE.METER_REFUND_CHK_STATUS" :value="scope.row.chkStatus"
          /></div>
          <div
            >退款：<dict-tag :type="DICT_TYPE.METER_REFUND_STATUS" :value="scope.row.refundStatus"
          /></div>
          <div
            >清零：<dict-tag
              :type="DICT_TYPE.METER_CLEAR_STATUS"
              :value="scope.row.clearMeterStatus"
          /></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120px">
        <template #default="scope">
          <el-button
            link
            type="danger"
            v-if="scope.row.chkStatus == 0"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zwsj:req-elec-charge-refund:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { formatDate } from '@/utils/formatTime'
import { divide } from '@/utils/tools'
import * as ReqElecChargeRefundApi from '@/api/zwsj/reqElecChargeRefund'

defineOptions({ name: 'ReqElecChargeRefund' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const keyWord = ref('')
const kType = ref('ownMemberMatCode')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  filterStatus: null,
  ownMemberMatCode: '',
  memberUserName: '',
  rentAddrName: ''
})
const statuses = ref([
  { value: 'waiting', label: '待处理' },
  { value: 'refunding', label: '待退款' },
  { value: 'clearing', label: '待清零' },
  { value: 'finished', label: '已完成' }
])
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.ownMemberMatCode = ''
    queryParams.memberUserName = ''
    queryParams.rentAddrName = ''
    if (kType.value == 'ownMemberMatCode') {
      queryParams.ownMemberMatCode = keyWord.value
    } else if (kType.value == 'memberUserName') {
      queryParams.memberUserName = keyWord.value
    } else if (kType.value == 'rentAddrName') {
      queryParams.rentAddrName = keyWord.value
    }
    const data = await ReqElecChargeRefundApi.getReqElecChargeRefundPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ReqElecChargeRefundApi.deleteReqElecChargeRefund(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
